package Tydzien2Test;
import static org.junit.Assert.*;

import org.junit.Test;

import Tydzien2.Tablice;

import java.util.Arrays;

public class TabliceTest {

	@Test
	public void testFillArray() {
		int[] tab = new int[7];
		Tablice.fillArray(tab, 3);
		int[] exact = {3,3,3,3,3,3,3};
		assertArrayEquals(tab, exact);
	}

	@Test
	public void testPrintArray() {
		int[] tab = {5,7,9,1,2,3,4};
		Tablice.printArray(System.out, tab);
	}

	@Test
	public void testFillArrayDescending() {
		int[] tab = new int[15];
		Tablice.fillArrayDescending(tab);
		int[] exact = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6};
		assertArrayEquals(tab,exact);
	}

	@Test
	public void testCreateArray() {
		int[] tab = Tablice.createArray(10);
		int[] exact = {0,0,0,0,0,0,0,0,0,0};
		assertArrayEquals(tab, exact);
	}

	@Test
	public void testCreateRandomArray() {
		int[] tab = Tablice.createRandomArray(10);
		assertEquals(tab.length, 10);
		for(int i = 0; i < 10; ++i)
		{
			if(tab[i] < 0 || tab[i] >= 20)
			{
				fail(i+"-th element out of range.");
			}
		}
	}

	@Test
	public void testCreateRandomRangeArray() {
		int[] tab = Tablice.createRandomRangeArray(11, 17);
		assertEquals(tab.length, 11);
		for(int i = 0; i < 11; ++i)
		{
			if(tab[i] < 0 || tab[i] >= 17)
			{
				fail(i+"-th element out of range.");
			}
		}
	}
	
	@Test
	public void testBubbleSort() {
		int[] tab = Tablice.createRandomRangeArray(20, 50);
		System.out.println("");
		Tablice.printArray(System.out, tab);
		Tablice.bubbleSort(tab);
		Tablice.printArray(System.out, tab);
		for(int i = 0; i < 19; ++i)
		{
			if(tab[i] > tab[i+1])
			{
				fail(i + "-th and " + (i+1) + "-th element are not in order.");
			}
		}
	}
	
	@Test
	public void testSortingTimes() {
		int[][] tabBubble = {Tablice.createRandomRangeArray(10000, 3),
							 Tablice.createRandomRangeArray(20000, 100),
							 Tablice.createRandomRangeArray(100000, 1000)};
		int[][] tabSort = tabBubble;
		long start, end;
		System.out.println("");
		for(int i = 0; i < 3; ++i)
		{
			start = System.currentTimeMillis();
			Tablice.bubbleSort(tabBubble[i]);
			end = System.currentTimeMillis();
			System.out.println("Sortowanie babelkowe nr " + i + ": " + (end - start) + "ms");
			start = System.currentTimeMillis();
			Arrays.sort(tabSort[i]);
			end = System.currentTimeMillis();
			System.out.println("Sortowanie z klasy Arrays nr " + i + ": " + (end - start) + "ms");
		}
	}

}
